/**
 * 
 */
package de.cbf.cam.dsa.entity.edit.item.persister.db4o.client;

import java.util.Map;

import org.eclipse.jface.preference.IPreferenceStore;

import de.cbf.cam.dsa.entity.edit.item.persister.db4o.client.act.ActivatorEntityItem_Db4oClient;
import de.cbf.cam.dsa.entity.edit.item.resolver.persister_interface.IServicePersister_EntityItem;
import de.cbf.util.db.interfaces.exceptions.EntityLoadException;
import de.cbf.util.db.interfaces.exceptions.EntitySaveException;
import de.cbf.util.entity.facet.entities.EntityFacetReferences;
import de.cbf.util.entity.facet.persistence.db4o.AbstractReflectiveElementPersisterDB4O;
import de.cbf.util.uuidheader.entities.UUIDReference;
import de.cbf.util.uuidheader.interfaces.IUUIDHeader;

/**
 * @author Nebelritter
 *
 */
public class ServicePersister_EntityItem_Db4o extends AbstractReflectiveElementPersisterDB4O implements IServicePersister_EntityItem{

	public static final String DISPLAY_NAME = "Entity:Item:Db4o";

	@Override
	protected IPreferenceStore getPreferenceStoreFromActivator() {		
		return ActivatorEntityItem_Db4oClient.getDefault().getPreferenceStore();
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.entity.facet.persistence.db4o.AbstractPersisterElementDb4o#init()
	 */
	@Override
	protected void init() {		
		super.init();
		setDisplayName(DISPLAY_NAME);
	}

	@Override
	public void saveEntityItem(EntityFacetReferences entityTalent, Map<String, Object> options)
			throws EntitySaveException {
		persistElement(entityTalent,options);		
	}

	@Override
	public EntityFacetReferences loadEntityItem(UUIDReference reference)
			throws EntityLoadException {
		IUUIDHeader element = loadElement(reference);
		if (element instanceof EntityFacetReferences) {
			EntityFacetReferences entityFacetReferences = (EntityFacetReferences) element;
			return entityFacetReferences;
		} else {
			String err_msg = "entity of wrong type(expected "
					+ EntityFacetReferences.class.getSimpleName() + "): " + (element == null ? "null"
					: element.getClass().getSimpleName());
			throw new EntityLoadException(err_msg);
		}		
	}

	@Override
	protected Class<? extends IUUIDHeader> getPersistedClass() {		
		return EntityFacetReferences.class;
	}
	
}
